package com.example.smartcity_poverty.ui.News

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.smartcity_poverty.App
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.util.Util
import com.example.smartcity_poverty.ui.ShowNewsActivity
import com.example.smartcity_poverty.ui.Home.HomeViewModel
import com.example.smartcity_poverty.ui.Home.fragment.NewsFragment
import com.google.android.material.tabs.TabLayout
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_news.*

/**
 * 底部导航栏 - 新闻模块
 */
class NewsFragment : Fragment() {

    private val viewModel by lazy {
        ViewModelProviders.of(this)[HomeViewModel::class.java]
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_news, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // Banner
        setBanner()


        // new
        setNews()
    }

    private fun setNews() {
        val arr = listOf<String>(
            "今日要闻",
            "专题聚焦",
            "政策解读",
            "经济发展",
            "文化旅游",
            "科技创新"
        )

        for (i in arr){
            nnTabLayout.addTab(nnTabLayout.newTab().setText(i))
        }

        // VewPager
        nnViewPager.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int = 6

            override fun createFragment(position: Int): Fragment = when(position){
                0 -> NewsFragment()
                1 -> NewsFragment()
                2 -> NewsFragment()
                3 -> NewsFragment()
                4 -> NewsFragment()
                else -> NewsFragment()
            }

            override fun getItemId(position: Int): Long {
                NewsFragment.newsId = when(position){
                    0 -> 9
                    1 -> 17
                    2 -> 19
                    3 -> 20
                    4 -> 21
                    else -> 22
                }
                return super.getItemId(position)
            }
        }

        // 监听ViewPager的滑动切换TabLayout标签
        nnViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                nnTabLayout.getTabAt(position)?.select()
            }
        })

        // 监听TabLayout点击，切换ViewPager2页面
        nnTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                nnViewPager.currentItem = tab?.position!!
            }

        })
    }

    private fun setBanner() {
        val arr = ArrayList<String>()

        viewModel.setBanner(mapOf("pageNum" to 1, "pageSize" to 8, "type" to 2))
        viewModel.bannerLiveData.observe(this, Observer { resources ->
            val body = resources.getOrNull()
            if (body != null){
                viewModel.bannerList.clear()
                viewModel.bannerList.addAll(body.rows!!)
            }

            for (i in viewModel.bannerList){
                arr.add(App.url + i.advImg)
            }

            mmBanner.apply {
                this.adapter = object : BannerImageAdapter<String>(arr){
                    override fun onBindView(p0: BannerImageHolder?, p1: String?, p2: Int, p3: Int) {
                        Util.glide(p1.toString(), p0?.imageView!!)
                    }
                }
                this.addBannerLifecycleObserver(this@NewsFragment).indicator = CircleIndicator(context)
                this.setBannerRound(25f)
                this.setOnBannerListener { _, i ->
                    if (i == 0){
                        startActivity(Intent(context, ShowNewsActivity::class.java).apply {
                            putExtra("id", "25")
                        })
                    }

                    if (i == 1){
                        startActivity(Intent(context, ShowNewsActivity::class.java).apply {
                            putExtra("id", "26")
                        })
                    }

                    if (i == 2){
                        startActivity(Intent(context, ShowNewsActivity::class.java).apply {
                            putExtra("id", "27")
                        })
                    }

                }
            }
        })
    }
}